package de.hdu.pms.ho.dao;


import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import de.hdu.pms.ho.model.Kategorie;

/**
 * This is the class which contains all necessary functions with {@link Kategorie} content
 * @author Thomas Niedermeier
 *
 */
public class KategorieDao extends HibernateDaoSupport {

	public static Logger Log = Logger.getLogger(KategorieDao.class);
	
	/**
	 * Creates a new {@link Kategorie} for testing class
	 * @param b
	 * @param p
	 */
	public void create(String b, double p) {
		Kategorie k = new Kategorie(b,p);
		save(k);
	}
	
	
	/**
	 * saves a specified object of kategorie into the database, either a new one will be written into the database or an existing will be updated
	 * @param kategorie an object of {@link Kategorie} which should be saved
	 * @return the {@link Kategorie} object
	 */
	public Kategorie save(Kategorie kategorie) {
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(kategorie);
		
		return kategorie;
	}
	

	
	/**
	 * Deletes a specified object from the class kategorie from the database
	 * @param kategorie is the object which should be deleted
	 */
	public void delete(Kategorie kategorie) {
		HibernateTemplate template = getHibernateTemplate();
		template.delete(kategorie);
	}
	
	/**
	 * Returns a single category containing to the selected id
	 * @param id is the primary key of {@link Kategorie}
	 * @return of a single category
	 */
	public Kategorie findById(Integer id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Kategorie) template.get(Kategorie.class, id);
	}
	
	/**
	 * Returns all categories saved in the database
	 * @return all objects of the class {@link Kategorie}
	 */
	public List<Kategorie> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(Kategorie.class);
	}
	
}
